package com.task.web.controller.core;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import javax.annotation.Resource;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import com.task.common.annotation.Log;
import com.task.common.core.controller.BaseController;
import com.task.common.core.domain.AjaxResult;
import com.task.common.core.page.TableDataInfo;
import com.task.common.core.redis.RedisCache;
import com.task.common.enums.BusinessType;
import com.task.common.utils.SecurityUtils;
import com.task.core.domain.MissionInfo;
import com.task.core.dto.request.MissionGetRequestDTO;
import com.task.core.dto.request.MissionRequestDTO;
import com.task.core.service.CheckService;
import com.task.core.service.MissionService;

import io.swagger.annotations.Api;

/**
 * @author 迪迦.
 * @date 2024/11/4 14:31
 */
@Api("")
@RestController
@RequestMapping("/mission")
public class MissionController extends BaseController {

  @Resource
  private MissionService missionService;

  @Resource
  private RedisCache redisCache;

  @Resource
  private CheckService checkService;


  @PreAuthorize("@ss.hasPermi('mission:list')")
  @GetMapping("/getMissionByPage")
  public AjaxResult getMissionByPage(MissionRequestDTO request) {
    startPage(request.getPageNum(), request.getPageSize());
    return success(missionService.getMissionByPage(request));
  }


  @GetMapping("/getMissionByMissionCode/{missionCode}")
  public AjaxResult getMissionByMissionCode(@PathVariable String missionCode) {
    return success(missionService.getMissionDetailByCode(missionCode ,true));
  }


  @PreAuthorize("@ss.hasPermi('mission:option')")
  @GetMapping("/getMissionForOption")
  public TableDataInfo getMissionForOption(MissionRequestDTO request) {
    return getDataTable(missionService.getMissionForOption(request));
  }

  @Log(title = "任务管理", businessType = BusinessType.INSERT)
  @PostMapping("/addMission")
  public AjaxResult addMission(@RequestBody MissionRequestDTO request) {
    request.setOperationBy(SecurityUtils.getLoginUser().getUserId());
    missionService.addMission(request);
    return success();
  }

  @Log(title = "任务管理", businessType = BusinessType.UPDATE)
  @PutMapping("/modifyMission")
  public AjaxResult modifyMission(@RequestBody MissionRequestDTO request) {
    checkService.check("data_mission", request.getCode());
    request.setOperationBy(SecurityUtils.getLoginUser().getUserId());
    missionService.modifyMission(request);
    return success();
  }

  @Log(title = "任务管理", businessType = BusinessType.UPDATE)
  @PutMapping("/initMissionNormalState/{missionCode}")
  public AjaxResult initMissionNormalState(@PathVariable String missionCode) {
    missionService.initMissionNormalState(missionCode, true);
    return success();
  }

  @Log(title = "任务管理", businessType = BusinessType.UPDATE)
  @PutMapping("/changeMissionStatus/{missionCode}/{status}")
  public AjaxResult changeMissionStatus(@PathVariable String missionCode, @PathVariable boolean status) {
    missionService.changeMissionStatus(missionCode, status);
    return success();
  }

  @RequestMapping("/test")
  public AjaxResult test(@RequestBody MissionRequestDTO request) {
    Map<String, Object> map = new HashMap<>();
    redisCache.redisTemplate.opsForHash().put("allKey:1:asdasd", "key1", 1);
    redisCache.redisTemplate.opsForHash().put("allKey:1:asdasd", "key2", 2);
    redisCache.redisTemplate.opsForHash().put("allKey:1:asdasd", "key3", "Name1");
    redisCache.redisTemplate.opsForHash().put("allKey:1:zxczxc", "key1", 1);
    redisCache.redisTemplate.opsForHash().put("allKey:1:zxczxc", "key2", 2);
    redisCache.redisTemplate.opsForHash().put("allKey:1:zxczxc", "key3", "Name2");
    return success();
  }

  @RequestMapping("/testGet")
  public AjaxResult testGet(@RequestBody MissionRequestDTO request) {
    Collection<String> keys = redisCache.keys("allKey:1:*");
    List<String> collect = keys.stream().map(f -> f.split(":")[2]).collect(Collectors.toList());
    List<Object> objects = new ArrayList<>();
    for (String s : collect) {
      objects.add(redisCache.getCacheMap("allKey:1:" + s));
    }
    return success(objects);
  }

  @RequestMapping("/testAdd")
  public AjaxResult testAdd(@RequestBody MissionRequestDTO request) {
    return success(redisCache.redisTemplate.opsForHash().increment("allKey:1:asdasd", "key1", 100));
  }

  @Log(title = "任务管理", businessType = BusinessType.DELETE)
  @DeleteMapping("/deleteMission/{missionCode}")
  public AjaxResult deleteMission(@PathVariable String missionCode) {
    checkService.check("data_mission", missionCode);
    missionService.deleteMission(missionCode, SecurityUtils.getLoginUser().getUserId());
    return success();
  }

  @Log(title = "任务管理", businessType = BusinessType.DELETE)
  @DeleteMapping("/deleteMissionBatch")
  public AjaxResult deleteMissionBatch(List<String> missionCodes) {
    for (String missionCode : missionCodes) {
      checkService.check("data_mission", missionCode);
    }
    for (String missionCode : missionCodes) {
      missionService.deleteMission(missionCode, SecurityUtils.getLoginUser().getUserId());
    }
    return success();
  }

  @Log(title = "任务管理", businessType = BusinessType.UPDATE)
  @PutMapping("/initMission")
  public AjaxResult initMission(@RequestParam("missionCode") String missionCode) {
    missionService.initMission(missionCode);
    return success();
  }


}
